package org.redmagic.service.controller.ui;

import org.redmagic.service.domain.ui.DisplayProperty;
import org.redmagic.service.ui.RedMagicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class CreatePropertyController {

	private static final String CREATE_FORM_VIEW = "createPropertyForm";
	private static final String PROPERTY_FORM_ATTRIBUTE = "propertyForm";
	
	@Autowired
	private RedMagicService redMagicService;

	@RequestMapping(value="/createPropertyForm", method=RequestMethod.GET)
	public ModelAndView createPropertyForm() {
		return new ModelAndView(CREATE_FORM_VIEW, PROPERTY_FORM_ATTRIBUTE, new DisplayProperty());
	}
	
	@RequestMapping(value="/createProperty", method=RequestMethod.POST)
	public String createProperty(@ModelAttribute(PROPERTY_FORM_ATTRIBUTE) DisplayProperty displayProperty) {
		this.redMagicService.create(displayProperty);
		return "redirect:/viewAll";
	}
}